/*
	PlAr is Platform Arena: a 2D multiplayer shooting game
	Copyright (c) 2010, Antonio Ragagnin <spocchio@gmail.com>
    All rights reserved.

    This file is licensed under the New BSD License.
*/

package plar.core;
import org.jbox2d.dynamics.*;


/**
 * This is a kind of element that disapper when touched
 * used to implements ammo and heath refill
 *
 * @author Antonio Ragagnin
 *
 */
public abstract class ElementGadget extends Element {

/**
* this class implements {@link ActionTouch} that manage collsion from objects.
* this is used to manage the collision of the gadget, and to made it disappear when a player touch it.
*/
    public class CollisionPlayer extends ActionTouch
    {

	/**
	* @param t1 duration of the timer
	*/
        public CollisionPlayer(float t1)
        {
            super(t1);
            active=false;
        }
        public  boolean collision(Element me, Element e) {

            if(!active) ((ElementGadget)me).elementCollided(e);
            return !active;

        }

        public void touched() {
            if( spritechange) {
                active=true;

                ((ElementGadget)me).oldSpriteSet = me.currentSpriteSet;
                me.currentSpriteSet = null;
            }
        }

        public void timeOut() {
            if( spritechange) {
                active=false;
                me.currentSpriteSet = ((ElementGadget)me).oldSpriteSet;
            }
        }






    }


	/**
	* time gap from the reappearing of the gadget after his disappearing
	*/
    float timegap;
	
	/**
	* information about the status of the gadget, e.g. visible or not.. do not modify
	*/
    public int info;
	
	/**
	* the spriteset of the element before it was touched and became hidden
	*/
    public SpriteSet oldSpriteSet;
	
	/**
	* true if the sprite will change after a collision with the element
	*/
    public boolean spritechange;
	
	
    CollisionPlayer cp;
    public ElementGadget() {
        super();
        spritechange=true;
        timegap=10;
        type=Common.ElementType.GADGET;
        isRunnable=true;
        cp = new CollisionPlayer(timegap);
        actions.addAction("collision", cp);
    }

    public void setTimeGap(float t)
    {
        cp.setTime(t);
    }
    public void setTimer(boolean t)
    {
        cp.setTimer(t);
    }

    public void setFixtureDef(FixtureDef fd)
    {
        super.setFixtureDef(fd);

        fd.isSensor=true; //generate excpetions on world.step ();

    }
    public abstract void elementCollided(Element e);


}
